package com.mmche.web.controller;



import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easycoding.framework.annotation.Token;
import com.framework.base.util.CookieUtil;
import com.framework.base.util.Tool;
import com.framework.support.domain.PageMan;
import com.framework.ums.common.CommonService;
import com.framework.ums.domain.Dic;
import com.framework.web.springmvc.BaseController;
import com.mmche.base.pojo.Brand;
import com.mmche.base.pojo.Models;
import com.mmche.base.pojo.ReadRecord;
import com.mmche.base.service.BrandService;
import com.mmche.base.service.ModelsService;
import com.mmche.base.service.ReadRecordService;
import com.mmche.car.pojo.Car;
import com.mmche.car.service.CarService;
import com.mmche.common.Constant;
import com.mmche.sell.pojo.WantOrder;
import com.mmche.sell.service.SellOrderService;
import com.mmche.sell.service.WantOrderService;
import com.mmche.web.pojo.SearchCnd;

@Controller
public class BuyController extends BaseController{
	
	@Resource(name="webCarService")
	private CarService carService;
	
	@Resource(name="webBrandService")
	private BrandService brandService;
	
	@Resource
	private ModelsService modelsService;
	
	@Resource
	private ReadRecordService readRecordService;
	
	@Resource
	private WantOrderService wantOrderService;
	
	@Resource(name="webSellOrderService")
	private SellOrderService sellOrderService;
	
	@RequestMapping("/buycar")
	private String buycar(Map<String,Object> returnValue,String[] params){
		returnValue.put("carTypes", CommonService.getInstance().getDics(Constant.DIC_CAR_TYPE));
		returnValue.put("tranTypes", CommonService.getInstance().getDics(Constant.DIC_TRANSMISSION_TYPE));
		returnValue.put("colors",CommonService.getInstance().getDics(Constant.DIC_CAR_COLOR));
		//搜索车辆
		PageMan pageMan = new PageMan();
	    pageMan.setPageSize(15);
	    Map<String,Object> paramMap = new HashMap<String,Object>();
	    String keywords = "";
	    Map<String,SearchCnd> cnds = new LinkedHashMap<String,SearchCnd>();//记录搜索条件值
	    returnValue.put("cnds", cnds);
	    if(!Tool.isEmpty(params)){
	    	for(String param : params)
	    		keywords = this.analyseParam(param, keywords, returnValue, paramMap, cnds,pageMan);
	    }
	    searchCar(paramMap,keywords,cnds,pageMan,returnValue);
		return "buy/buycar";
	}
	
	@RequestMapping("/{param}")
	private String buycar(@PathVariable String param,Map<String,Object> returnValue){
		return buycar(returnValue,new String[]{param});
	}
	
	@RequestMapping("/{param1}/{param2}")
	private String buycar(@PathVariable String param1,@PathVariable String param2,Map<String,Object> returnValue){
		return buycar(returnValue,new String[]{param1,param2});
	}

	@RequestMapping("/{param1}/{param2}/{param3}")
	private String buycar(@PathVariable String param1,@PathVariable String param2,@PathVariable String param3,Map<String,Object> returnValue){
		return buycar(returnValue,new String[]{param1,param2,param3});
	}
	
	@RequestMapping("/{param1}/{param2}/{param3}/{param4}")
	private String buycar(@PathVariable String param1,@PathVariable String param2,@PathVariable String param3,@PathVariable String param4,Map<String,Object> returnValue){
		return buycar(returnValue,new String[]{param1,param2,param3,param4});
	}
	
	/**
	 * 车辆信息
	 * @param id
	 * @param returnValue
	 * @return
	 */
	@RequestMapping("/car/{id}.html")
	private String car(@PathVariable String id,Map<String,Object> returnValue){
		returnValue.put("location", "buycar");
    	Car car = this.carService.getCarById(id);
    	if(car == null){
			id = id.toUpperCase();
			logger.info("车辆对象为空，ID小写转大写"+id);
			car = this.carService.getCarById(id);
			logger.info("车辆对象："+car);
		}
    	returnValue.put("car", car);
    	this.carService.addCarCtr(car);
		//浏览记录
		ReadRecord readRecord = new ReadRecord();
		readRecord.setReadId(car.getId());
		readRecord.setType(ReadRecord.TYPE_CAR);
		readRecordService.saveReadRecord(readRecord);
		HttpServletRequest request = this.getWebApplicationContext().getRequest();
		HttpServletResponse response = this.getWebApplicationContext().getResponse();
		Cookie cookie = CookieUtil.getCookie(request, "viewdHistory");
		if(cookie == null){
			CookieUtil.setCookie(request, response, "viewdHistory", id+",", 30*24*60*60, "/");
		}
		else{
			String history = cookie.getValue();
			String[] cars = history.replaceAll(id+",", "").split(",");
			returnValue.put("viewdCars", this.carService.findViewdCar(cars,10));
			history = id + ",";
			for(int i=0 ; i < cars.length && i < 9 ; i++){
				if(!Tool.isEmpty(cars[i])){
					history += cars[i] + ",";
				}
			}
			CookieUtil.setCookie(request, response, "viewdHistory", history, 30*24*60*60, "/");
		}
		returnValue.put("hotCars",this.carService.getHotCars(10));
		Map<String,Object> map = new HashMap<String,Object>();
		//促销车辆
		map.put("status", Car.STATUS_SHELVES);
	    map.put("promo", true);
	    returnValue.put("promoCars",this.carService.getCarPageByMap(map, 0, 10));
		return "buy/car";
	}
	
	/**
	 * 我要求购
	 * @param returnValue
	 * @return
	 */
	@RequestMapping("/buy.html")
	@Token(create = true)
	private String buy(Map<String,Object> returnValue){
		returnValue.put("location", "buycar");
		returnValue.put("brands", this.brandService.getCarBrands());
		returnValue.put("colors", CommonService.getInstance().getDics(Constant.DIC_CAR_COLOR));
		List<String> limitYear = new ArrayList<String>();
		limitYear.add("不限");
		for(int i=1;i<9;i++){
			limitYear.add(i+"年以下");
		}
		limitYear.add("8年以上");
		returnValue.put("years",limitYear);
		return "buy/buy";
	}
	
	/**
	 * 保存求购信息
	 * @param want
	 * @return
	 */
	@RequestMapping("/buy/save.html")
	@Token(validate = true)
	private String want(WantOrder want){
		if(want != null){
			try{
				String brand = this.getRequestParameter("brandId");
				String series = this.getRequestParameter("seriesId");
				if(!Tool.isEmpty(brand))
					want.setBrand(this.brandService.findBySpell(brand));
				if(!Tool.isEmpty(series))
					want.setModels(this.modelsService.getModelsById(series));
				this.wantOrderService.save(want);
			}
			catch(Exception e){
				logger.error("保存求购意向单出错!",e);
			}
		}
		return "redirect:/buy/success.html";
	}
	
	/**
	 * 保存求购信息成功
	 * @param returnValue
	 * @return
	 */
	@RequestMapping("/buy/success.html")
	public String success(Map<String,Object> returnValue){
		returnValue.put("location", "buycar");
		return "buy/success";
	}
	
	/**
	 * 解析参数
	 * @param param
	 * @param keywords
	 * @param returnValue
	 * @param paramMap
	 * @param cnds
	 * @param pageMan 
	 * @return
	 */
	private String analyseParam(String param,String keywords,Map<String,Object> returnValue,Map<String,Object> paramMap,Map<String,SearchCnd> cnds, PageMan pageMan){
		@SuppressWarnings("unchecked")
		List<Dic> carTypes = (List<Dic>) returnValue.get("carTypes");
		@SuppressWarnings("unchecked")
		List<Dic> tranTypes = (List<Dic>) returnValue.get("tranTypes");
		@SuppressWarnings("unchecked")
		List<Dic> colors = (List<Dic>) returnValue.get("colors");
		if(param.startsWith("P")){//页码参数
			String pageNo = param.substring(1);
			pageMan.setPageNo(Integer.parseInt(pageNo));
			SearchCnd cnd = new SearchCnd("页码",param,"第" + pageNo + "页");
	    	cnds.put("page", cnd);
	    }
		else{
			Brand brand = this.brandService.findBySpell(param);
		    if(brand != null){//品牌
		    	returnValue.put("serieses",this.modelsService.getModels(brand.getId()));
		    	keywords += " 品牌:"+brand.getName();
		    	SearchCnd cnd = new SearchCnd("品牌",brand.getEnname(),brand.getName());
		    	cnds.put("brand", cnd);
		    }
		    else {
		    	Models model = this.modelsService.findBySpell(param);
		    	if(model != null){
		    		keywords += " 车系:"+model.getName();
			    	SearchCnd cnd = new SearchCnd("车系",model.getEnname(),model.getName());
			    	cnds.put("series", cnd);
		    	}
			    else{//不是品牌，解析参数
			    	getPriceParam(paramMap,param,cnds);//获得价格
			    	getAgeParam(paramMap,param,cnds);//获得车龄
			    	keywords = getTypeParam(keywords,param,cnds,carTypes);//获得车型
			    	keywords = getTranParam(keywords,param,cnds,tranTypes);//变速箱
			    	keywords = getPLParam(keywords,param,cnds);//获得排量
			    	keywords = getColorParam(keywords,param,cnds,colors);//获得排量
			    	getOrderParam(paramMap,param,cnds);//排序
			    }
		    }
		}
	    return keywords;
	}
	
	/**
	 * 查找车辆
	 * @param paramMap
	 * @param pageMan 
	 * @param returnValue
	 */
	private void searchCar(Map<String,Object> paramMap,String keywords,Map<String,SearchCnd> cnds , PageMan pageMan, Map<String,Object> returnValue){
		returnValue.put("location", "buycar");
		returnValue.put("brands",this.brandService.getIndexBrands());
		if(!Tool.isEmpty(keywords)){
	    	paramMap.put("keywords", keywords);
		}
		paramMap.put("status", Car.STATUS_SHELVES);
		List<Car> result = this.carService.getCarPageByMap(paramMap, pageMan);
	    returnValue.put("cars",result);
	    if(Tool.isEmpty(result)){
	    	SearchCnd brand = cnds.get("brand");
	    	if(brand != null){//如果选择了品牌
	    		PageMan page = new PageMan();
	    		page.setPageSize(15);
	    	    Map<String,Object> map = new HashMap<String,Object>();
	    	    String key = " 品牌:"+brand.getValue();
	    	    map.put("keywords", key);
	    	    map.put("status", Car.STATUS_SHELVES);
	    	    returnValue.put("hotCars", this.carService.getCarPageByMap(map, page));
	    	}
	    	else
	    		returnValue.put("hotCars",this.carService.getHotCars(15));
	    }
	    returnValue.put("pageMan",pageMan);
	    //TDK设置
	    this.setTDK(cnds,returnValue);
//	    SearchCnd page = cnds.get("page");
//	    if(page != null){
//	    	page.setKey("P" + pageMan.getPageNo());
//	    	page.setValue("第" + pageMan.getPageNo() + "页");
//	    }
	    this.getNextUrl(returnValue,paramMap,cnds);
		Map<String,Object> map = new HashMap<String,Object>();
		//促销车辆
		map.put("status", Car.STATUS_SHELVES);
	    map.put("promo", true);
	    returnValue.put("promoCars",this.carService.getCarPageByMap(map, 0, 4));
	    //浏览记录
	    HttpServletRequest request = this.getWebApplicationContext().getRequest();
		Cookie cookie = CookieUtil.getCookie(request, "viewdHistory");
		if(cookie != null){
			String history = cookie.getValue();
			String[] cars = history.split(",");
			returnValue.put("viewdCars",this.carService.findViewdCar(cars,4));
		}
	}

	/**
	 * 获得车龄参数
	 * @param paramMap
	 * @param param
	 */
	private void getAgeParam(Map<String,Object> paramMap,String param,Map<String,SearchCnd> cnds){
		if(!Tool.isEmpty(param)){
			//车龄
			if(param.contains("A1")){//1年以内
				Date maxDate = Tool.dateAddYear(new Date(), -1);
				SimpleDateFormat df = new SimpleDateFormat("yyyy");
				String startYear = df.format(maxDate);
				paramMap.put("startYear", startYear);
				cnds.put("age", new SearchCnd("车龄","A1","1年以内"));
			}
			else if(param.contains("A2")){//2年以内
				Date maxDate = Tool.dateAddYear(new Date(), -2);
				SimpleDateFormat df = new SimpleDateFormat("yyyy");
				String startYear = df.format(maxDate);
				paramMap.put("startYear", startYear);
				cnds.put("age", new SearchCnd("车龄","A2","2年以内"));
			}
			else if(param.contains("A3")){//3年以内
				Date maxDate = Tool.dateAddYear(new Date(), -3);
				SimpleDateFormat df = new SimpleDateFormat("yyyy");
				String startYear = df.format(maxDate);
				paramMap.put("startYear", startYear);
				cnds.put("age", new SearchCnd("车龄","A3","3年以内"));
			}
			else if(param.contains("A4")){//3-5年
				SimpleDateFormat df = new SimpleDateFormat("yyyy");
				Date minDate = Tool.dateAddYear(new Date(), -3);
				String endYear = df.format(minDate);
				paramMap.put("endYear", endYear);
				Date maxDate = Tool.dateAddYear(new Date(), -5);
				String startYear = df.format(maxDate);
				paramMap.put("startYear", startYear);
				cnds.put("age", new SearchCnd("车龄","A4","3年以内"));
			}
			else if(param.contains("A5")){//5-8年
				SimpleDateFormat df = new SimpleDateFormat("yyyy");
				Date minDate = Tool.dateAddYear(new Date(), -5);
				String endYear = df.format(minDate);
				paramMap.put("endYear", endYear);
				Date maxDate = Tool.dateAddYear(new Date(), -8);
				String startYear = df.format(maxDate);
				paramMap.put("startYear", startYear);
				cnds.put("age", new SearchCnd("车龄","A5","5-8年"));
			}
			else if(param.contains("A6")){//8年以上
				SimpleDateFormat df = new SimpleDateFormat("yyyy");
				Date minDate = Tool.dateAddYear(new Date(), -8);
				String endYear = df.format(minDate);
				paramMap.put("endYear", endYear);
				cnds.put("age", new SearchCnd("车龄","A6","8年以上"));
			}
		}
	}
	
	/**
	 * 获得价格参数
	 * @param paramMap
	 * @param param
	 */
	private void getPriceParam(Map<String,Object> paramMap,String param,Map<String,SearchCnd> cnds){
		if(!Tool.isEmpty(param)){
			//价格
			if(param.contains("H1")){//3万以下
				paramMap.put("endPrice", 3 + "");
				cnds.put("price", new SearchCnd("价格","H1","3万以下"));
			}
			else if(param.contains("H2")){//3-5万
				paramMap.put("startPrice", 3 + "");
				paramMap.put("endPrice", 5 + "");
				cnds.put("price", new SearchCnd("价格","H2","3-5万"));
			}
			else if(param.contains("H3")){//5-8万
				paramMap.put("startPrice", 5 + "");
				paramMap.put("endPrice", 8 + "");
				cnds.put("price", new SearchCnd("价格","H3","5-8万"));
			}
			else if(param.contains("H4")){//8-10万
				paramMap.put("startPrice", 8 + "");
				paramMap.put("endPrice", 10 + "");
				cnds.put("price", new SearchCnd("价格","H4","8-10万"));
			}
			else if(param.contains("H5")){//10-15万
				paramMap.put("startPrice", 10 + "");
				paramMap.put("endPrice", 15 + "");
				cnds.put("price", new SearchCnd("价格","H5","10-15万"));
			}
			else if(param.contains("H6")){//15-20万
				paramMap.put("startPrice", 15 + "");
				paramMap.put("endPrice", 20 + "");
				cnds.put("price", new SearchCnd("价格","H6","15-20万"));
			}
			else if(param.contains("H7")){//20-50万
				paramMap.put("startPrice", 20 + "");
				paramMap.put("endPrice", 50 + "");
				cnds.put("price", new SearchCnd("价格","H7","20-50万"));
			}
			else if(param.contains("H8")){//50万以上
				paramMap.put("startPrice", 50 + "");
				cnds.put("price", new SearchCnd("价格","H8","50万以上"));
			}
		}
	}
	
	/**
	 * 获得车型参数
	 * @param paramMap
	 * @param param
	 * @param carTypes 
	 * @param params
	 */
	private String getTypeParam(String keywords,String param,Map<String,SearchCnd> cnds, List<Dic> carTypes){
		if(!Tool.isEmpty(param)){
			//车型
			for(int i=0;i < carTypes.size() ; i++){
				if(param.contains("T" + (i+1))){
					keywords += " 类型:" + carTypes.get(i).getDesc();
					cnds.put("type", new SearchCnd("类型" ,"T" + (i+1),carTypes.get(i).getDesc()));
					break;
				}
			}
		}
		return keywords;
	}
	
	/**
	 * 获得排序参数
	 * @param paramMap
	 * @param param
	 * @param cnds
	 */
	private void getOrderParam(Map<String,Object> paramMap,String param,Map<String,SearchCnd> cnds){
		if(!Tool.isEmpty(param)){
			//排序
			if(param.contains("U1")){//发布日期升序
				paramMap.put("sort", "shelvesTime");
				paramMap.put("sortType", "0");
				cnds.put("sort", new SearchCnd("排序","U1","发布日期升序"));
			}
			else if(param.contains("D1")){//发布日期降序
				paramMap.put("sort", "shelvesTime");
				paramMap.put("sortType", "1");
				cnds.put("sort", new SearchCnd("排序","D1","发布日期降序"));
			}
			else if(param.contains("U2")){//价格升序
				paramMap.put("sort", "online.price");
				paramMap.put("sortType", "1");
				cnds.put("sort", new SearchCnd("排序","U2","价格升序"));
			}
			else if(param.contains("D2")){//价格降序
				paramMap.put("sort", "online.price");
				paramMap.put("sortType", "0");
				cnds.put("sort", new SearchCnd("排序","D2","价格降序"));
			}
			else if(param.contains("U3")){//车龄升序
				paramMap.put("sort", "registerTime");
				paramMap.put("sortType", "0");
				cnds.put("sort", new SearchCnd("排序","U3","车龄升序"));
			}
			else if(param.contains("D3")){//车龄降序
				paramMap.put("sort", "registerTime");
				paramMap.put("sortType", "1");
				cnds.put("sort", new SearchCnd("排序","D3","车龄降序"));
			}
			else if(param.contains("U4")){//里程升序
				paramMap.put("sort", "mileage");
				paramMap.put("sortType", "1");
				cnds.put("sort", new SearchCnd("排序","U4","里程升序"));
			}
			else if(param.contains("D4")){//里程降序
				paramMap.put("sort", "mileage");
				paramMap.put("sortType", "0");
				cnds.put("sort", new SearchCnd("排序","D4","里程降序"));
			}
		}
	}
	
	/**
	 * 获得变速箱参数
	 * @param paramMap
	 * @param param
	 * @param cnds
	 */
	private String getTranParam(String keywords,String param,Map<String,SearchCnd> cnds,List<Dic> tranTypes){
		if(!Tool.isEmpty(param)){
			//变速箱
			for(int i=0;i < tranTypes.size() ; i++){
				if(param.contains(tranTypes.get(i).getCode())){
					keywords += " 变速箱:" + tranTypes.get(i).getDesc();
					cnds.put("tran", new SearchCnd("变速箱" ,tranTypes.get(i).getCode(),tranTypes.get(i).getDesc()));
					break;
				}
			}
		}
		return keywords;
	}
	
	/**
	 * 获得排量参数
	 * @param paramMap
	 * @param param
	 * @param cnds
	 * @return 
	 */
	private String getPLParam(String keywords,String param,Map<String,SearchCnd> cnds){
		if(!Tool.isEmpty(param)){
			//排量
			if(param.contains("E1")){//发布日期升序
				keywords += " 排量:1.0L以下";
				cnds.put("pl", new SearchCnd("排量","E1","1.0L以下"));
			}
			else if(param.contains("E2")){//发布日期升序
				keywords += " 排量:1.1-1.5L";
				cnds.put("pl", new SearchCnd("排量","E2","1.1-1.5L"));
			}
			else if(param.contains("E3")){//发布日期升序
				keywords += " 排量:1.6-2.0L";
				cnds.put("pl", new SearchCnd("排量","E3","1.6-2.0L"));
			}
			else if(param.contains("E4")){//发布日期升序
				keywords += " 排量:2.1-2.5L";
				cnds.put("pl", new SearchCnd("排量","E4","2.1-2.5L"));
			}
			else if(param.contains("E5")){//发布日期升序
				keywords += " 排量:2.5-3.0L";
				cnds.put("pl", new SearchCnd("排量","E5","2.5-3.0L"));
			}
			else if(param.contains("E6")){//发布日期升序
				keywords += " 排量:3.0L以上";
				cnds.put("pl", new SearchCnd("排量","E6","3.0L以上"));
			}
		}
		return keywords;
	}
	
	/**
	 * 
	 * @param keywords
	 * @param param
	 * @param cnds
	 * @param colors
	 * @return
	 */
	private String getColorParam(String keywords, String param,
			Map<String, SearchCnd> cnds, List<Dic> colors) {
		if(!Tool.isEmpty(param)){
			//颜色
			for(int i=0;i < colors.size() ; i++){
				if(param.contains("C" + (i+1))){
					keywords += " 颜色:" + colors.get(i).getDesc();
					cnds.put("color", new SearchCnd("颜色" ,"C" + (i+1),colors.get(i).getDesc()));
					break;
				}
			}
		}
		return keywords;
	}
	
	/**
	 * 根据当前参数，设置下一个页面的url
	 */
	public void getNextUrl(Map<String,Object> returnValue, Map<String,Object> paramMap ,Map<String,SearchCnd> cnds){
		String brandCnd = "";//品牌
		String seriesCnd = "";//车系
		String priceCnd = "";//价格
		String ageCnd = "";//车龄
		String typeCnd = "";//车身结构
		String tranCnd = "";//变速箱
		String plCnd = "";//排量
		String colorCnd = "";//颜色
		String sortCnd = "";//排序
		String pageCnd = "";//页码
		if(cnds != null){
		    SearchCnd brand = cnds.get("brand");
		    if(brand != null){
		    	brandCnd = brand.getKey();
		    	returnValue.put("showCnds", true);
		    }
		    SearchCnd series = cnds.get("series");
		    if(series != null){
		    	seriesCnd = series.getKey();
		    	returnValue.put("showCnds", true);
		    }
		    SearchCnd price = cnds.get("price");
		    if(price != null){
		    	priceCnd = price.getKey();
		    	returnValue.put("showCnds", true);
		    }
		    SearchCnd age = cnds.get("age");
		    if(age != null){
		    	ageCnd = age.getKey();
		    	returnValue.put("showCnds", true);
		    }
		    SearchCnd type = cnds.get("type");
		    if(type != null){
		    	typeCnd = type.getKey();
		    	returnValue.put("showCnds", true);
		    }
		    SearchCnd tran = cnds.get("tran");
		    if(tran != null){
		    	tranCnd = tran.getKey();
		    	returnValue.put("showCnds", true);
		    }
		    SearchCnd pl = cnds.get("pl");
		    if(pl != null){
		    	plCnd = pl.getKey();
		    	returnValue.put("showCnds", true);
		    }
		    SearchCnd color = cnds.get("color");
		    if(color != null){
		    	colorCnd = color.getKey();
		    	returnValue.put("showCnds", true);
		    }
		    SearchCnd sort = cnds.get("sort");
		    if(sort != null)
		    	sortCnd = sort.getKey();
//		    SearchCnd page = cnds.get("page");
//		    if(page != null)
//		    	pageCnd = page.getKey();
		}
		String brand_url = "";
		String series_url = "";
		String price_url = "";
		String age_url = "";
		String type_url = "";
		String tran_url = "";
		String pl_url = "";
		String color_url = "";
		String page_url = "";
		String sort_url = "";
		brand_url += "/BRAND";
		if(!Tool.isEmpty(brandCnd)){
			series_url += "/" + brandCnd;
			price_url += "/" + brandCnd;
			age_url += "/" + brandCnd;
			type_url += "/" + brandCnd;
			tran_url += "/" + brandCnd;
			pl_url += "/" + brandCnd;
			color_url += "/" + brandCnd;
			sort_url += "/" + brandCnd;
			page_url += "/" + brandCnd;
		}
		series_url += "/SERIES";
		if(!Tool.isEmpty(seriesCnd)){
			brand_url += "/" + seriesCnd;
			price_url += "/" + seriesCnd;
			age_url += "/" + seriesCnd;
			type_url += "/" + seriesCnd;
			tran_url += "/" + seriesCnd;
			pl_url += "/" + seriesCnd;
			color_url += "/" + seriesCnd;
			sort_url += "/" + seriesCnd;
			page_url += "/" + seriesCnd;
		}
		String separator = "/";
		price_url += separator + "PRICE";
		if(!Tool.isEmpty(priceCnd)){
			brand_url += separator + priceCnd;
			series_url += separator + priceCnd;
			age_url += separator + priceCnd;
			type_url += separator + priceCnd;
			tran_url += separator + priceCnd;
			pl_url += separator + priceCnd;
			color_url += separator + priceCnd;
			sort_url += separator + priceCnd;
			page_url += separator + priceCnd;
			separator = "_";
		}
		age_url += separator + "AGE";
		if(!Tool.isEmpty(ageCnd)){
			brand_url += separator + ageCnd;
			series_url += separator + ageCnd;
			price_url += "_" + ageCnd;
			type_url += separator + ageCnd;
			tran_url += separator + ageCnd;
			pl_url += separator + ageCnd;
			color_url += separator + ageCnd;
			sort_url += separator + ageCnd;
			page_url += separator + ageCnd;
			separator = "_";
		}
		type_url += separator + "TYPE";
		if(!Tool.isEmpty(typeCnd)){
			brand_url += separator + typeCnd;
			series_url += separator + typeCnd;
			price_url += "_" + typeCnd;
			age_url += "_" + typeCnd;
			tran_url += separator + typeCnd;
			pl_url += separator + typeCnd;
			color_url += separator + typeCnd;
			sort_url += separator + typeCnd;
			page_url += separator + typeCnd;
			separator = "_";
		}
		tran_url += separator + "TRAN";
		if(!Tool.isEmpty(tranCnd)){
			brand_url += separator + tranCnd;
			series_url += separator + tranCnd;
			price_url += "_" + tranCnd;
			age_url += "_" + tranCnd;
			type_url += "_" + tranCnd;
			pl_url += separator + tranCnd;
			color_url += separator + tranCnd;
			sort_url += separator + tranCnd;
			page_url += separator + tranCnd;
			separator = "_";
		}
		pl_url += separator + "PL";
		if(!Tool.isEmpty(plCnd)){
			brand_url += separator + plCnd;
			series_url += separator + plCnd;
			price_url += "_" + plCnd;
			age_url += "_" + plCnd;
			type_url += "_" + plCnd;
			tran_url += "_" + plCnd;
			color_url += separator + plCnd;
			sort_url += separator + plCnd;
			page_url += separator + plCnd;
			separator = "_";
		}
		color_url += separator + "COLOR";
		if(!Tool.isEmpty(colorCnd)){
			brand_url += separator + colorCnd;
			series_url += separator + colorCnd;
			price_url += "_" + colorCnd;
			age_url += "_" + colorCnd;
			type_url += "_" + colorCnd;
			tran_url += "_" + colorCnd;
			pl_url += "_" + colorCnd;
			sort_url += separator + colorCnd;
			page_url += separator + colorCnd;
			separator = "_";
		}
		sort_url += separator + "SORT";
		if(!Tool.isEmpty(sortCnd)){
			brand_url += separator + sortCnd;
			series_url += separator + sortCnd;
			price_url += "_" + sortCnd;
			age_url += "_" + sortCnd;
			type_url += "_" + sortCnd;
			tran_url += "_" + sortCnd;
			pl_url += "_" + sortCnd;
			color_url += "_" + sortCnd;
			page_url += separator + sortCnd;
			separator = "_";
		}
		page_url += "/PPAGE";
		if(!Tool.isEmpty(pageCnd)){
			brand_url += "/" + pageCnd;
			series_url += "/" + pageCnd;
			price_url += "/" + pageCnd;
			age_url += "/" + pageCnd;
			type_url += "/" + pageCnd;
			tran_url += "/" + pageCnd;
			pl_url += "/" + pageCnd;
			color_url += "/" + pageCnd;
			sort_url += "/" + colorCnd;
		}
		brand_url += "/";
		series_url += "/";
		price_url += "/";
		age_url += "/";
		type_url += "/";
		tran_url += "/";
		pl_url += "/";
		color_url += "/";
		sort_url += "/";
		returnValue.put("brand_url", brand_url);
    	returnValue.put("series_url", series_url);
    	returnValue.put("price_url", price_url);
    	returnValue.put("age_url", age_url);
    	returnValue.put("type_url", type_url);
    	returnValue.put("tran_url", tran_url);
    	returnValue.put("pl_url", pl_url);
    	returnValue.put("color_url", color_url);
    	returnValue.put("sort_url", sort_url);
    	returnValue.put("page_url", page_url);
    	String all_brand_url = brand_url.replace("/BRAND", "");
    	if(!Tool.isEmpty(seriesCnd))
    		all_brand_url = all_brand_url.replace("/" + seriesCnd , "");
		String all_series_url = series_url.replace("/SERIES", "");
		String all_price_url = price_url.replace("PRICE_", "").replace("PRICE", "").replace("_/", "/").replace("//", "/");
		String all_age_url = age_url.replace("AGE_", "").replace("AGE", "").replace("_/", "/").replace("//", "/");
		String all_type_url = type_url.replace("TYPE_", "").replace("TYPE", "").replace("_/", "/").replace("//", "/");
		String all_tran_url = tran_url.replace("TRAN_", "").replace("TRAN", "").replace("_/", "/").replace("//", "/");
		String all_pl_url = pl_url.replace("PL_", "").replace("PL", "").replace("_/", "/").replace("//", "/");
		String all_color_url = color_url.replace("COLOR_", "").replace("COLOR", "").replace("_/", "/").replace("//", "/");
		String all_sort_url = sort_url.replace("SORT_", "").replace("SORT", "").replace("_/", "/").replace("//", "/");
		all_brand_url = all_brand_url.equals("/") ? "/buycar/" : all_brand_url;
		all_series_url = all_series_url.equals("/") ? "/buycar/" : all_series_url;
		all_price_url = all_price_url.equals("/") ? "/buycar/" : all_price_url;
		all_age_url = all_age_url.equals("/") ? "/buycar/" : all_age_url;
		all_type_url = all_type_url.equals("/") ? "/buycar/" : all_type_url;
		all_tran_url = all_tran_url.equals("/") ? "/buycar/" : all_tran_url;
		all_pl_url = all_pl_url.equals("/") ? "/buycar/" : all_pl_url;
		all_color_url = all_color_url.equals("/") ? "/buycar/" : all_color_url;
		all_sort_url = all_sort_url.equals("/") ? "/buycar/" : all_sort_url;
    	returnValue.put("all_brand_url", all_brand_url);
    	returnValue.put("all_series_url", all_series_url);
    	returnValue.put("all_price_url", all_price_url);
    	returnValue.put("all_age_url", all_age_url);
    	returnValue.put("all_type_url", all_type_url);
    	returnValue.put("all_tran_url", all_tran_url);
    	returnValue.put("all_pl_url", all_pl_url);
    	returnValue.put("all_color_url", all_color_url);
    	returnValue.put("all_sort_url", all_sort_url);
    	if(cnds != null){
		    SearchCnd brand = cnds.get("brand");
		    if(brand != null)
		    	 brand.setDelUrl(all_brand_url);
		    SearchCnd series = cnds.get("series");
		    if(series != null)
		    	series.setDelUrl(all_series_url);
		    SearchCnd price = cnds.get("price");
		    if(price != null)
		    	price.setDelUrl(all_price_url);
		    SearchCnd age = cnds.get("age");
		    if(age != null)
		    	age.setDelUrl(all_age_url);
		    SearchCnd type = cnds.get("type");
		    if(type != null)
		    	type.setDelUrl(all_type_url);
		    SearchCnd tran = cnds.get("tran");
		    if(tran != null)
		    	tran.setDelUrl(all_tran_url);
		    SearchCnd pl = cnds.get("pl");
		    if(pl != null)
		    	pl.setDelUrl(all_pl_url);
		    SearchCnd color = cnds.get("color");
		    if(color != null)
		    	color.setDelUrl(all_color_url);
		}
	}
	/**
	 * 设置TDK
	 * @param cnds
	 * @param returnValue
	 */
	private void setTDK(Map<String, SearchCnd> cnds,
			Map<String, Object> returnValue) {
		String city = this.getWebApplicationContext().getRequest().getSession().getAttribute("city").toString();
	    String title = "";
	    String description = "";
	    String keyword = "";
	    String brand = cnds.get("brand") == null ? "" : cnds.get("brand").getValue();
	    String series = cnds.get("series") == null ? "" : cnds.get("series").getValue();
	    String price = cnds.get("price") == null ? "" : cnds.get("price").getValue();
	    String age = cnds.get("age") == null ? "" : cnds.get("age").getValue();
	    String car = Tool.isEmpty(series) ? brand : series;
	    if(!Tool.isEmpty(price) || !Tool.isEmpty(age)){
	    	title = "【";
	    	if(!Tool.isEmpty(age)){
	    		title += city + age + "二手" + car +"_";
	    		keyword += city + age + "二手车,";
	    	}
	    	if(!Tool.isEmpty(price)){
	    		title += city + price  + car +"二手车报价";
	    		keyword += city + price + "二手车,";
	    	}
	    	title = (title + "】").replace("_】", "】");
	    	title += "_麦麦车二手车网络超市";
	    	keyword = keyword.substring(0,keyword.length()-1);
	    	description = city + "二手" + price + age + car +",麦麦车二手车网络超市提供最新"
	    			+ city + price + age + car + "认证二手车,包含" + city + "二手" + price + age + car + "报价及图片信息,"
	    			+ city + price +age + car + "认证二手车价格多少钱,详解二手" + price + age +car + "图片,车源对比功能,"
	    			+ "帮您选择适合您的" + city + price + age + car+ "认证二手车。";
	    }
	    else if(!Tool.isEmpty(series)){
	    	title = "【" + city + "二手" + series + "_" + city + series + "二手车报价】_麦麦车二手车网络超市";
	    	keyword = city + series + "认证二手车," +city + series + "认证二手车价格,"
	    			+ city + "二手" + series + "价格," + city + series +"认证二手车多少钱";
	    	description = city + "二手" + series + ",麦麦车二手车网络超市提供最新" + city + series +"认证二手车,"
	    			+ "包含" + city + "二手" + series + "报价及图片信息," + city + series + "认证二手车价格多少钱,"
	    			+ "详解二手" + series + "图片,车源对比功能,帮您选择适合您的" + city + series +"认证二手车。";
	    }
	    else if(!Tool.isEmpty(brand)){
	    	title = "【" + city + "二手" + brand +"_" + city + brand + "认证二手车报价及图片_认证二手" + brand + "价格】_麦麦车二手车网络超市";
	    	keyword = city + "二手" + brand + "报价," + city + brand + "二手车多少钱";
	    	description = city + "二手" + brand + ",麦麦车二手车网络超市提供最新" + city + brand +"认证二手车,"
	    				+ "包含" + city + "二手" + brand + "报价及图片信息," + city + brand + "认证二手车价格多少钱,"
	    				+ "详解二手" + brand + "图片,车源对比功能,帮您选择适合您的" + city + brand + "认证二手车。";
	    }
	    else{
	    	title = "【" + city + "二手车价格_" + city + "认证二手车_" + city + "二手车买卖,报价及图片】_麦麦车二手车网络超市";
	    	keyword = city + "二手车价格," + city + "二手车买卖," + "二手车报价," + city + "二手车图片";
	    	description = "麦麦车二手车网络超市为您精选" + city + "最新的优质认证二手车车源，涵盖不同等级、年份、里程和价格，提供专业比价工具，透明一口价买卖，七天质量保障，为您提供" + city + "最可靠的认证二手车报价。";
	    }
	    returnValue.put("title", title);
	    returnValue.put("description", description);
	    returnValue.put("keyword", keyword);
	}
	
	/**
	 * 验证订单号是否正确
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/validOrderId.html")
	public @ResponseBody JSONArray CompareFactorIdValidate(HttpServletRequest request) throws Exception{
		String validateField = request.getParameter("fieldId");
	    String validateValue = request.getParameter("fieldValue"); //获取域的value属性
	    JSONArray content =new JSONArray();
		content.add(0,validateField);
	    if(this.sellOrderService.vaildOrderId(validateValue.trim()))
	    	content.add(1,true);
	    else
	    	content.add(1,false);
	    return content;
	}
}

